{% if contest and user %}
<hr/>
<script>
    function autoAdd(fldName, type, extra, max){
        max = max || Infinity;
        var elems = [];
        var fldElem = function(){
            var newFld = document.createElement('li');
            var fldClone = document.createElement('input');
            fldClone.type = type;
            fldClone.name = fldName;
            if(extra)
                fldClone.setAttribute(extra,extra);
            newFld.appendChild(fldClone);
            return newFld;
        }
        var container = document.createElement('ul');
        var w = document.getElementsByTagName('*');
        var p = w[w.length-1].parentNode;
        p.appendChild(container);
        elems.push(container.appendChild(fldElem()).firstChild);
        setInterval(function(){try{
            if (elems.length < max && elems[elems.length-1].value!='')
                elems.push(container.appendChild(fldElem()).firstChild);
            for (var i=1;i<elems.length-1;i++){
                if(elems[i].value=='')try{
                    container.removeChild(elems[i].parentNode);
                }catch(e){}
            }
        }catch(e){}},500);
    }
</script>
<form method="POST" enctype="multipart/form-data" {% if headless %} action="?" target="_blank" {% endif %}>
    {% if not submission or submission.creator.profile == user.profile %}
        <input type="hidden" name="action" value="new"/>
        {% if submission %}
            <h3 id="editar">Editar</h3>
        {% else %}
            <h3 id="nuevo">Nuevo</h3>
        {% endif %}
        <label for="name">Nombre *</label>
        <input value="{{ submission.name }}" type="text" id="name" name="name" class="input_title" required /> <br/>
        <label for="description">Descripci&oacute;n * (sin html/bbcode)</label>
        <div id="toolbar"></div>
        <div id="editDesc"></div>
        <textarea id="description" required name="description" class="input_desc">{{ submission.description }}</textarea>
        <label>Logo (50x50)</label>
        <ul style="display: block; clear: both;">
            {% if submission and submission.image_set.count %}
                <li class="radioChoose">
                {% for image in submission.image_set %}
                    <div class="avatar_item">
                        <label for="avatar_{{ image.key }}">
                            <img src="{{ image.url }}{% if not image.original_url %}/50/50{% endif %}" alt="{{ image.caption }}" title="{{ image.caption }}" class="icon" onerror="style.height=style.width='100%';" />
                        </label>
                        <input type="radio" name="avatar_opt" value="{{ image.key }}"  id="avatar_{{ image.key }}"
                            {% ifequal submission.icon.key image.key %}
                                checked
                            {% endifequal %}
                        />
                    </div>
                {% endfor %}
                <label> O agrega una nueva imagen :</label> <br/>
                </li>
            {% endif %}
            <div><input type="file" name="avatar_file"/></div>
        </ul>
        <div style="clear: both;">&nbsp;</div>
        <hr/>
        <label>Links/Descargas</label>
        <script>autoAdd('links','text');</script>
        <label>Imagenes (max 1MB por imagen)</label>
        <script>autoAdd('images','file','multiple', 9);</script>
        {% if videos %}
        <label>Videos (Youtube)</label>
        <script>autoAdd('videos','text');</script>
        {% endif %}
        <input type="hidden" name="token" value="{{ token }}"/>
        <input type="submit" value="Guardar"/>
    {% else %}
        <input type="hidden" name="token" value="{{ token }}"/>
        <input type="hidden" name="action" value="vote"/>
        <input type="submit" value="Votalo!"/>
    {% endif %}
</form>
{% endif %}